﻿Public Class BeheerWinkeliersForm

    Private _mainForm As Form

#Region "Constructor"

    Public Sub New(ByVal mainForm As Form)
        Me.InitializeComponent()
        _mainForm = mainForm
    End Sub

    Protected Overrides Sub OnLoad(ByVal e As EventArgs)
        MyBase.OnLoad(e)
        Try
            Me.WinkeliersBindingSource.DataSource = Utility.Data
            InitGrid()
            tsbBewaren.Enabled = False
            tsbVerwijderen.Enabled = False
        Catch ex As Exception
            MsgBox.ShowError("Ophalen data is mislukt")
            log.WriteError("BeheerWinkeliersForm.Load" & Environment.NewLine & ex.ToString())
        End Try
    End Sub

#End Region

#Region "Public property's"

    Private _kanBewaren As Boolean
    Public Property KanBewaren() As Boolean
        Get
            Return _kanBewaren
        End Get
        Set(ByVal value As Boolean)
            _kanBewaren = value
            tsbBewaren.Enabled = value
        End Set
    End Property

    Private _kanVerwijderen As Boolean
    Public Property KanVerwijderen() As Boolean
        Get
            Return _kanVerwijderen
        End Get
        Set(ByVal value As Boolean)
            _kanVerwijderen = value
            tsbVerwijderen.Enabled = value
            BindingNavigatorDeleteItem.Enabled = value
        End Set
    End Property

#End Region

#Region "Event handling"

    Protected Overrides Sub OnKeyUp(ByVal e As KeyEventArgs)
        MyBase.OnKeyUp(e)
        Select Case e.KeyCode
            Case Keys.F12
                Me.Close()
            Case Keys.F2
                Me.WinkeliersBindingSource.EndEdit()
                If (Not KanBewaren) Then Return
                Bewaar()
            Case Keys.F3
                If (Not KanVerwijderen) Then Return
                Verwijderen()
        End Select
    End Sub

    Private Sub tsbVerwijderen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbVerwijderen.Click
        Verwijderen()
    End Sub

    Private Sub tsbBewaren_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbBewaren.Click
        Bewaar()
    End Sub

    Private Sub tsbSluiten_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbSluiten.Click
        Me.Close()
    End Sub

    Protected Overrides Sub OnFormClosing(ByVal e As FormClosingEventArgs)
        MyBase.OnFormClosing(e)
        Me.Validate()
        Me.WinkeliersBindingSource.EndEdit()
        If (Utility.HasChanges(Utility.Data.Winkeliers)) Then
            Dim result As DialogResult = MessageBox.Show("De lijst met winkeliers is gewijzigd. Wil je de wijzigingen bewaren?", "Bewaren", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation)
            Select Case result
                Case Windows.Forms.DialogResult.Yes
                    Bewaar()
                Case Windows.Forms.DialogResult.Cancel
                    e.Cancel = True
                    Return
                Case Windows.Forms.DialogResult.No
                    ' annuleer de wijzigingen en laat sluiten
                    Utility.Data.Winkeliers.RejectChanges()
            End Select
        End If
        _mainForm.BringToFront()
    End Sub

    Private Sub DataGridView1_CurrentCellDirtyStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Grid1.CurrentCellDirtyStateChanged
        KanBewaren = True
    End Sub

    Private Sub Grid1_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) _
            Handles Grid1.SelectionChanged
        KanVerwijderen = (Grid1.SelectedRows.Count > 0)
    End Sub

#End Region

#Region "Helperfuncties"

    Private Sub InitGrid()
        With DataGridViewHelper.GetGridColumnByDataName(Utility.Data.Categorieen.IdColumn.ColumnName, Grid1)
            .Visible = False ' Autonumber niet tonen
        End With
        With DataGridViewHelper.GetGridColumnByDataName(Utility.Data.Winkeliers.NaamColumn.ColumnName, Grid1)
            .Width = 300
        End With
        With DataGridViewHelper.GetGridColumnByDataName(Utility.Data.Winkeliers.WachtwoordColumn.ColumnName, Grid1)
            .Width = 300
        End With
    End Sub

    Private Shadows Function Bewaar() As Integer
        Return MyBase.Bewaar(Utility.Data.Winkeliers, "winkelier(s)", KanBewaren)
    End Function

    Private Sub Verwijderen()
        Try
            If (Me.WinkeliersBindingSource.Current IsNot Nothing) Then
                Me.WinkeliersBindingSource.RemoveCurrent()
                KanBewaren = True
            End If
        Catch ex As Exception
            log.WriteError("BeheerWinkeliersForm.Verwijderen" & Environment.NewLine & ex.ToString())
        End Try
    End Sub

#End Region

End Class